package com.ouheng.shop.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.shop.domain.MemberDO;
import com.ouheng.shop.domain.RedcodeDO;
import com.ouheng.shop.service.MemberService;
import com.ouheng.shop.service.RedcodeService;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;

/**
 * 红包
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-10-08 10:13:38
 */
 
@Controller
@RequestMapping("/shop/redcode")
public class RedcodeController {
	@Autowired
	private RedcodeService redcodeService;
	
	@Autowired
	private MemberService memberService;
	
	@GetMapping()
	String Redcode(Long id,Model model){
		model.addAttribute("memberid", id);
	    return "shop/redcode/redcode";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<RedcodeDO> redcodeList = redcodeService.list(query);
		List<RedcodeDO> redcodeList1=new ArrayList<>();
		for (RedcodeDO redcodeDO : redcodeList) {
			if(!"1".equals(redcodeDO.getType())){
				redcodeDO.setNickname("公司");
				redcodeDO.setImage("http://aleiaapi.oss-cn-qingdao.aliyuncs.com/image/875703880606499465.jpg");
			}else{
				MemberDO memberDO = memberService.get(redcodeDO.getZmemberid());
				redcodeDO.setNickname(memberDO.getNickname());
				redcodeDO.setImage(memberDO.getImage());
			}
			redcodeList1.add(redcodeDO);
		}
		
		int total = redcodeService.count(query);
		PageUtils pageUtils = new PageUtils(redcodeList1, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:redcode:add")
	String add(){
	    return "shop/redcode/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:redcode:edit")
	String edit(@PathVariable("id") Long id,Model model){
		RedcodeDO redcode = redcodeService.get(id);
		model.addAttribute("redcode", redcode);
	    return "shop/redcode/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:redcode:add")
	public R save( RedcodeDO redcode){
		if(redcodeService.save(redcode)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:redcode:edit")
	public R update( RedcodeDO redcode){
		redcodeService.update(redcode);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:redcode:remove")
	public R remove( Long id){
		if(redcodeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:redcode:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		redcodeService.batchRemove(ids);
		return R.ok();
	}
	
}
